package com.sit.aicodehelper.config;

import com.sit.aicodehelper.rag.RagConfig;
import com.sit.aicodehelper.service.AiCodeHelperService;
import com.sit.aicodehelper.tools.InterviewQuestionTool;
import dev.langchain4j.mcp.McpToolProvider;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.StreamingChatModel;
import dev.langchain4j.rag.content.retriever.ContentRetriever;
import dev.langchain4j.service.AiServices;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AiCodeHelperServiceFactory {
    @Resource
    private ChatModel chatModel;
    @Resource
    private ContentRetriever contentRetriever;
    @Resource
    private McpToolProvider mcpToolProvider;
    @Resource
    private StreamingChatModel qwenStreamingChatModel;

    @Bean
    public AiCodeHelperService aiCodeHelperService() {
        // 会话记忆
        MessageWindowChatMemory chatMemory = MessageWindowChatMemory.withMaxMessages(10);
        return AiServices.builder(AiCodeHelperService.class)
                // 模型
                .chatModel(chatModel)
                // 会话记忆
                .chatMemory(chatMemory)
                // 会话记忆提供者,每个会话独立存储
                .chatMemoryProvider(memoryId -> MessageWindowChatMemory.withMaxMessages(10))
                // 模型流式
                .streamingChatModel(qwenStreamingChatModel)
                // RAG检索增强生成
                .contentRetriever(contentRetriever)
                // 工具调用
                .tools(new InterviewQuestionTool())
                // MCP 工具调用
                .toolProvider(mcpToolProvider)
                .build();
    }
}
